package com.though.utils.encrypt;

import com.though.utils.exception.SysException;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.SortedMap;


/**
 * 签名工具
 */
public class SignUtils {

	
	public static String SIGN = "sign";
	
	
	
	/**
	 * 检测用户请求参数签名
	 */
	public static boolean checkSign(String app_name, SortedMap<String, String> params) {
		if (StringUtils.isBlank(app_name) || params == null || params.size() == 0) return false;
		String sign_key = AppKey.getAppKey(app_name);
		if (StringUtils.isBlank(sign_key)) throw new SysException("无效app名称请求！");
		String origin_sign = StringUtils.EMPTY;
		StringBuffer buffer = new StringBuffer();
		
		for (Map.Entry<String, String> entry : params.entrySet()) {
			if (entry.getKey().equals(SIGN)) {
				origin_sign = entry.getValue();
				continue;
			}
			buffer.append(entry.getValue());
		}
		
		buffer.append(sign_key);
		String new_sign = MD5.encode(buffer.toString());
		return new_sign.equals(origin_sign);
	}
}
